﻿var LoginPanel = function (rHd) {
    var __hd = (typeof (rHd) == "string") ? rHd : "loginPanel";
    var __succeedDerict = null;
    this.onNameFocus = function (sender, e) {
        if (!__ready) { return false; }
        if (__name.value == "") {
            __sname.value = "";
        } else {
            __sname.value = __name.value;
        }
        return true;
    };
    this.onNameBlur = function (sender, e) {
        if (!__ready) { return false; }
        __name.value = sender.value;
        if (__name.value == "") {
            __sname.value = __defNameTxt;
        }
        return true;
    };
    this.onPwdFocus = function (sender, e) {
        if (!__ready) { return false; }
        __pwd.style.display = "block";
        __pwd.focus();
        __spwd.style.display = "none";
        return true;
    };
    this.onPwdBlur = function (sender, e) {
        if (!__ready) { return false; }
        if (sender.value == "") {
            __pwd.style.display = "none";
            __spwd.style.display = "block";
        }
        return true;
    };
    this.onKeyDown = function (sender, e) {
        if (e.keyCode == 13) {
            this.doLogin();
        }
    };
    var ajax = null;
    this.doLogin = function () {
        __closeInfo();
        var u = __name.value;
        var p = __pwd.value;
        var vc = __imgCode.value;
        if (u == "" || p == "") { return false; }
        if (vc == "") {
            __imgCode = "请输入校验码";
            return false;
        }
        __info.className = "loginpanel_info_loading";
        __displayInfo("信息验证中，请稍后...");
        var url = "Ws/_Login.aspx?u=" + u + "&p=" + p + "&cookie=" + __cookie.checked + "&vc=" + vc + "&ra=" + jskitUtil.guid();
        ajax = new JskitXmlHttpAction(url, __hd + ".loginCallback", "text");
    };
    var __displayInfo = function (msg) {
        __info.innerHTML = msg;
        __info.style.display = "block";
        //__info.style.top = (__info.parentNode.offsetHeight -__info.offsetHeight)/2;
        //__info.style.left = (__info.parentNode.offsetWidth -__info.offsetWidth)/2;
    };
    var __closeInfo = function () {
        __info.style.display = "none";
    };
    this.closeInfo = function () {
        __closeInfo();
    };
    this.loginCallback = function (json) {
        __info.className = "loginpanel_info";
        ajax = null;
        var _data = null;
        try {
            eval("_data = " + json + ";");
        } catch (e) {
            __displayInfo("错误的连接请求，登录失败，请与管理员联系");
            alert(json);
            setTimeout(__hd + ".closeInfo()", 4000);
            return;
        }
        if (_data.msg != "") {
            __displayInfo(_data.msg);
            setTimeout(__hd + ".closeInfo()", 4000);
        } else {
            __info.className = "loginpanel_info_loading";
            __displayInfo("校验完成，正在转到相应的页面...");
            if (__succeedDerict != null) {
                setTimeout(__hd + ".gotoUrl(\"" + __succeedDerict + "\")", 2000);
            } else if (_data.url != "") {
                setTimeout(__hd + ".gotoUrl(\"" + _data.url + "\")", 2000);
            } else {
                setTimeout("window.location.reload()", 2000);
            }
        }
    };
    this.gotoUrl = function (rUrl) {
        __closeInfo();
        window.location.href = rUrl;
    };

    this.changeCodeImg = function (rUrl) {
        if (typeof (rUrl) == "string" && rUrl != "") {
            __img.src = rUrl + "?id=" + jskitUtil.guid();
        } else {
            __img.src = "Ws/_ValidateCodeImg.aspx?id=" + jskitUtil.guid();
        }
    };

    this.setLoginProperty = function (v) {
        __loginProperty = v;
    };
    this.setSucceedDerict = function (v) {
        if (typeof (v) == "string" && v !== "") {
            __succeedDerict = v;
        }
    };
    var __loginProperty = "LoginName";
    var __ready = false;
    var __defNameTxt = (__loginProperty == "Email") ? "请输入Email" : "请输入账号";
    var __defPwdTxt = "请输入密码";
    var __name = null;
    var __pwd = null;
    var __sname = null;
    var __spwd = null;
    var __cookie = null;
    var __info = null;
    var __imgCode = null;
    var __img = null;
    this.init = function () {
        __name = document.getElementById("hid_Name");
        __pwd = document.getElementById("pwd_Pwd");
        __sname = document.getElementById("txt_Name");
        __spwd = document.getElementById("txt_Pwd");
        __sname.value = __defNameTxt;
        __spwd.value = __defPwdTxt;
        __cookie = document.getElementById("cbx_Cookie");
        __info = document.getElementById("lab_info");
        __imgCode = document.getElementById("txt_ValidateCode");
        __img = document.getElementById("img_ValidateCode");
        __ready = true;
    };
};
